package com.learn.xiaoha.ProxyMapper;

import com.learn.xiaoha.sqlsession.annotion.SimpleSelect;
import com.learn.xiaoha.sqlsession.annotion.SimpleUpdate;
import com.learn.xiaoha.sqlsession.utils.JdbcExecute;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * SimpleMapperHandle
 *
 * @author: cyx_jay
 * @date: 2021/2/2 14:22
 */
public class SimpleMapperHandle implements InvocationHandler {


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //获取方法上的注解
        SimpleSelect simpleSelect = method.getAnnotation(SimpleSelect.class);
        if (simpleSelect != null){
            String sql = simpleSelect.value();
            return JdbcExecute.executeSelect(sql,method,args);
        }
        SimpleUpdate simpleUpdate = method.getAnnotation(SimpleUpdate.class);
        if (simpleUpdate != null){
            String sql = simpleUpdate.value();
            return JdbcExecute.executeUpdate(sql,method,args);
        }



      /*    //获取返回类型
        Class<?> returnType = method.getReturnType();
        Class actualType = null;
        if (returnType== List.class){
            Type type = method.getGenericReturnType();
            if (type instanceof ParameterizedType){
                Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
                //因为list泛型只有一个值 所以直接取0下标
                String typeName = actualTypeArguments[0].getTypeName();
                //真实返回值类型 Class对象
                actualType = Class.forName(typeName);
            }
        }*/

        return null;

    }
}
